package com.xilihui.xlh.business.activitys;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.xilihui.xlh.R;
import com.xilihui.xlh.business.entities.MsgEntivity;
import com.xilihui.xlh.business.requests.HomeRequest;
import com.xilihui.xlh.component.recyclerview.BaseAdapter;
import com.xilihui.xlh.component.recyclerview.BaseViewHolder;
import com.xilihui.xlh.core.app.BaseCompatActivity;
import com.xilihui.xlh.core.exception.DefaultExceptionListener;
import com.xilihui.xlh.core.exception.ExceptionManager;
import com.xilihui.xlh.core.http.DoTransform;
import com.xilihui.xlh.core.http.HttpSubscriber;
import com.xilihui.xlh.core.util.ToastUtil;

import java.util.ArrayList;

import butterknife.BindView;
import retrofit2.http.POST;

/**
 * Created by Administrator on 2018/7/13.
 */

public class MsgActivity extends BaseCompatActivity implements OnRefreshListener, OnLoadMoreListener, View.OnClickListener {
    
    @BindView(R.id.smartRefreshLayout)
    SmartRefreshLayout smartRefreshLayout;
    @BindView(R.id.recyclerview)
    RecyclerView recyclerView;
    
    ExceptionManager exceptionManager;
    
    BaseAdapter<MsgEntivity.DataBean> baseAdapter;
    ArrayList<MsgEntivity.DataBean> data = new ArrayList<>();
    
    int page = 1;
    
    Intent intent;
    
    @Override
    public int getLayout() {
        return R.layout.activity_msg;
    }

    @Override
    public String initTitle() {
        return "消息";
        
    }

    @Override
    public void initUI(@Nullable Bundle savedInstanceState) {
        
        exceptionManager = new ExceptionManager(smartRefreshLayout,new DefaultExceptionListener(this)){

            @Override
            protected int getEmptyView() {
                return R.layout.empty_msg;
            }
        };
        
        exceptionManager.showEmpty();
        
        smartRefreshLayout.setOnRefreshListener(this);
        smartRefreshLayout.setOnLoadMoreListener(this);
        
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        baseAdapter = new BaseAdapter<MsgEntivity.DataBean>(this,data) {
            @Override
            protected int setLayoutId() {
                return R.layout.recycler_item_msg;
            }

            @Override
            protected void getView(BaseViewHolder holder, final MsgEntivity.DataBean entity, int position) {
                holder.setText(R.id.tv_titleType,entity.getTitle());
                holder.setText(R.id.tv_time,entity.getAdd_time());
                holder.setText(R.id.tv_title,entity.getRemark());
                holder.setText(R.id.tv_danhao,entity.getHint());
                
                final String type = entity.getType();
                ImageView iv = (ImageView)holder.getView(R.id.iv_type);
                if(type.equals("1")||type.equals("3")){
                    iv.setImageResource(R.mipmap.message_ic_yj);
                }else if(type.equals("2")){
                    iv.setImageResource(R.mipmap.message_ic_kf);
                }else if(type.equals("3")){
                    iv.setImageResource(R.mipmap.message_ic_yj);
                } else if(type.equals("4")){
                    iv.setImageResource(R.mipmap.message_ic_xx);
                } else if(type.equals("5")){
                    iv.setImageResource(R.mipmap.message_ic_xt);
                } else if(type.equals("6")){
                    iv.setImageResource(R.mipmap.message_ic_xt);
                }
                
                
                holder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if(type.equals("1")){
                            intent = new Intent(MsgActivity.this,BillingDetailsActivity.class);
                            intent.putExtra("id",entity.getFrom_id());
                            startActivity(intent);
                        }else if(type.equals("2")){
                            intent = new Intent(MsgActivity.this,BillingDetailsActivity.class);
                            intent.putExtra("id",entity.getFrom_id());
                            startActivity(intent);
                        }else if(type.equals("3")){
                            intent = new Intent(MsgActivity.this,ShowDetailActivity.class);
                            intent.putExtra("id",entity.getFrom_id());
                            startActivity(intent);
                        }else if(type.equals("4")){
                            intent = new Intent(MsgActivity.this,ShowDetailActivity.class);
                            intent.putExtra("id",entity.getFrom_id());
                            startActivity(intent);
                        }else if(type.equals("5")){
                            intent = new Intent(MsgActivity.this,HistoryRecordActivity.class);
                            startActivity(intent);
                        }else if(type.equals("6")){
                            intent = new Intent(MsgActivity.this,ApplyActivity.class);
                            startActivity(intent);
                        }
                    }
                });
                
                
            }
        };
        
        recyclerView.setAdapter(baseAdapter);
        
    }

    @Override
    public void initData() {
        getData(true);
    }
    
    public void getData(boolean isLoading){
        HomeRequest.msg(MsgActivity.this,page)
                .compose(DoTransform.<MsgEntivity>applyScheduler(this,isLoading))
                .compose(this.<MsgEntivity>bindToLifecycle())
                .subscribe(new HttpSubscriber<MsgEntivity>(this) {
                    @Override
                    protected void onSuccess(MsgEntivity entity) {
                        smartRefreshLayout.finishRefresh();
                        smartRefreshLayout.finishLoadMore();
                        
                        if(page==1){
                            data.clear();
                            if(entity.getData().size()<1){
                                exceptionManager.showEmpty();
                            }else {
                                exceptionManager.hide();
                            }
                        }
                        
                        data.addAll(entity.getData());
                        baseAdapter.setList(data);
                            
                        if(page>=entity.getPageCount()){
                            smartRefreshLayout.finishLoadMoreWithNoMoreData();
                        }else{
                            smartRefreshLayout.setNoMoreData(false);
                        }
                        
                    }

                    @Override
                    protected void onFail(String msg) {
                        smartRefreshLayout.finishRefresh();
                        smartRefreshLayout.finishLoadMore();
                        ToastUtil.toastShortNegative(msg);
                    }
                });
    }


    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        page = 1;
        getData(false);
    }

    @Override
    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
        page = page + 1;
        getData(false);
    }

    @Override
    public void onClick(View view) {
        
    }
}
